﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewAllTeamParticipationOfStudent : System.Web.UI.Page
    {
        string queryString;
        StudentAL studentAL;
        SportsAL sportsAL;
        Guid teamMemberId;
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 5;
        AdminAL adminAL;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                queryString = Request.QueryString.ToString();
                teamMemberId = Guid.Empty;
                bool convertableToBool = Guid.TryParse(queryString, out teamMemberId);
                if (convertableToBool)
                {
                    BindData();
                }
                else
                {
                    return;
                }
                
            }
        }

        private void BindData()
        {

            studentAL = new StudentAL();
            sportsAL = new SportsAL();
            adminAL = new AdminAL();
            List<TeamMemberDTO> allParticipationOfTeamMember = sportsAL.GetAllTeamParticipationOfTeamMemberByTeamMemberId(teamMemberId).ToList();
            List<TeamMemberDTO> allParticipationOfTeamMemberOfOnePage = new List<TeamMemberDTO>();
            //gvViewAllMembers.DataSource = allMembers;
            //gvViewAllMembers.DataBind();


            // get the total rows 
            double totalRows = (int)allParticipationOfTeamMember.Count;

            lblAllParticpatnOfStudentTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllParticpatnOfStudentCurrPage.Text = currentPageNumber.ToString();

            lblAllParticpatnOfStudentPage.Text = "Displaying Participation of a team member in all Team(s) - Page ";
            lblAllParticpatnOfStudentOf.Text = "  of  ";

            if (Convert.ToInt32(lblAllParticpatnOfStudentTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text) <= 0)
                {
                    btnAllParticpatnOfStudentFirst.Enabled = false;
                    btnAllParticpatnOfStudentPrev.Enabled = false;
                    btnAllParticpatnOfStudentNext.Enabled = false;
                    btnAllParticpatnOfStudentLast.Enabled = false;


                }
                else
                {
                    if (currentPageNumber == 1)
                    {
                        //allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange(0, Convert.ToInt32(totalRows));
                        btnAllParticpatnOfStudentFirst.Enabled = false;
                        btnAllParticpatnOfStudentPrev.Enabled = false;

                        if (Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text) <= 0)
                        {
                            btnAllParticpatnOfStudentFirst.Enabled = false;
                            btnAllParticpatnOfStudentPrev.Enabled = false;
                            btnAllParticpatnOfStudentNext.Enabled = false;
                            btnAllParticpatnOfStudentLast.Enabled = false;


                        }

                        if (Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text) == 1)
                        {
                            //allMembersOfOnePage = allMembers.GetRange(0, PAGE_SIZE);
                            allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange(0, Convert.ToInt32(totalRows));
                            btnAllParticpatnOfStudentNext.Enabled = false;
                            btnAllParticpatnOfStudentLast.Enabled = false;

                        }
                        else if (Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text) == 2)
                        {
                            allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange(0, PAGE_SIZE);
                            btnAllParticpatnOfStudentNext.Enabled = true;
                            btnAllParticpatnOfStudentLast.Enabled = true;



                        }


                    }

                    else if (currentPageNumber == 2)
                    {
                        allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange(PAGE_SIZE, (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE));
                        btnAllParticpatnOfStudentPrev.Enabled = true;
                        btnAllParticpatnOfStudentFirst.Enabled = true;
                        btnAllParticpatnOfStudentNext.Enabled = false;
                        btnAllParticpatnOfStudentLast.Enabled = false;



                    }


                    else
                    {

                        if (currentPageNumber == Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text))
                        {
                            allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange((currentPageNumber - 1) * PAGE_SIZE
                                , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE));

                            btnAllParticpatnOfStudentPrev.Enabled = true;
                            btnAllParticpatnOfStudentFirst.Enabled = true;
                            btnAllParticpatnOfStudentNext.Enabled = false;
                            btnAllParticpatnOfStudentLast.Enabled = false;

                        }
                        else
                        {
                            allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange((currentPageNumber - 1) * PAGE_SIZE, PAGE_SIZE);

                            btnAllParticpatnOfStudentNext.Enabled = true;
                            btnAllParticpatnOfStudentLast.Enabled = true;
                            btnAllParticpatnOfStudentPrev.Enabled = false;
                            btnAllParticpatnOfStudentFirst.Enabled = true;



                        }
                    }
                }
            }


            else
            {
                if (Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text) <= 0)
                {
                    btnAllParticpatnOfStudentFirst.Enabled = false;
                    btnAllParticpatnOfStudentPrev.Enabled = false;
                    btnAllParticpatnOfStudentNext.Enabled = false;
                    btnAllParticpatnOfStudentLast.Enabled = false;


                }

                else
                {
                    if (currentPageNumber == 1)
                    {
                        allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange(0, PAGE_SIZE);
                        btnAllParticpatnOfStudentFirst.Enabled = false;
                        btnAllParticpatnOfStudentPrev.Enabled = false;
                        btnAllParticpatnOfStudentNext.Enabled = true;
                        btnAllParticpatnOfStudentLast.Enabled = true;

                    }
                    else if (currentPageNumber == 2)
                    {
                        allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange(0, PAGE_SIZE);
                        btnAllParticpatnOfStudentFirst.Enabled = true;
                        btnAllParticpatnOfStudentPrev.Enabled = true;
                        btnAllParticpatnOfStudentNext.Enabled = true;
                        btnAllParticpatnOfStudentLast.Enabled = true;

                    }


                    else if (currentPageNumber == Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text))
                    {
                        allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange((currentPageNumber - 1) * PAGE_SIZE, (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE));
                        btnAllParticpatnOfStudentNext.Enabled = false;
                        btnAllParticpatnOfStudentLast.Enabled = false;
                        btnAllParticpatnOfStudentNext.Enabled = false;
                        btnAllParticpatnOfStudentLast.Enabled = false;

                    }
                    else
                    {
                        allParticipationOfTeamMemberOfOnePage = allParticipationOfTeamMember.GetRange((currentPageNumber - 1) * PAGE_SIZE, PAGE_SIZE);
                        btnAllParticpatnOfStudentNext.Enabled = true;
                        btnAllParticpatnOfStudentLast.Enabled = true;
                        btnAllParticpatnOfStudentFirst.Enabled = true;
                        btnAllParticpatnOfStudentPrev.Enabled = true;

                    }


                }
            }

            CreateDynamicTable(allParticipationOfTeamMemberOfOnePage);

        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE );

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllParticpatnOfStudentCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllParticpatnOfStudentCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllParticpatnOfStudentTotalPage.Text);
                    break;
            }


            BindData();
        }


        private void CreateDynamicTable(List<TeamMemberDTO> allParticipationOfTeamMemberOfOnePage)
        {
            PlaceHolder4.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties
           

            int tblRows = allParticipationOfTeamMemberOfOnePage.Count;

            List<StudentDTO> studentDTO = new List<StudentDTO>();
            List<StaffDTO> staffDTO = new List<StaffDTO>();
            List<TeamDTO> teamDTO = new List<TeamDTO>();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder4.Controls.Add(tbl);

            TableRow trHeader = new TableRow();
            trHeader.Width = new Unit("25%");

            HyperLink hlTeamParticptnHeaderTeamName = new HyperLink();
            hlTeamParticptnHeaderTeamName.Text = "Team Name";

            HyperLink hlTeamParticptnCellHeaderYr = new HyperLink();
            hlTeamParticptnCellHeaderYr.Text = "Year";

            HyperLink hlTeamParticptnCellHeaderDivision = new HyperLink();
            hlTeamParticptnCellHeaderDivision.Text = "Age Division";


            HyperLink hlTeamParticptnCellHeaderRole = new HyperLink();
            hlTeamParticptnCellHeaderRole.Text = "Role";
                       
            List<HyperLink> allHeaderHyperlinks = new List<HyperLink>();

            //based  on selected type change column orders

            allHeaderHyperlinks.Add(hlTeamParticptnHeaderTeamName);
            allHeaderHyperlinks.Add(hlTeamParticptnCellHeaderYr);
            allHeaderHyperlinks.Add(hlTeamParticptnCellHeaderDivision);
            allHeaderHyperlinks.Add(hlTeamParticptnCellHeaderRole);



            foreach (HyperLink hyperlinkHeader in allHeaderHyperlinks)
            {
                TableCell tcHeader = new TableCell();
                tcHeader.BackColor = System.Drawing.Color.DarkSlateBlue;
                tcHeader.ForeColor = System.Drawing.Color.White;

                tcHeader.Controls.Add(hyperlinkHeader);
                trHeader.Cells.Add(tcHeader);



            }

            tbl.Rows.Add(trHeader);


            // Now iterate through the table and add your controls 
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");

                teamDTO = sportsAL.GetTeamById(allParticipationOfTeamMemberOfOnePage[i].TeamId).ToList();
                studentDTO = studentAL.GetStudentById(allParticipationOfTeamMemberOfOnePage[i].TeamMemberId).ToList();
                staffDTO = adminAL.GetStaffById(allParticipationOfTeamMemberOfOnePage[i].TeamMemberId).ToList();

                HyperLink hlTeamParticpatnCellTeam = new HyperLink();
                hlTeamParticpatnCellTeam.Text = teamDTO[0].AgeDivision + ", " + teamDTO[0].GameType + " Team";

                HyperLink hlTeamParticpatnCellYr = new HyperLink();
                hlTeamParticpatnCellYr.Text = teamDTO[0].Year.ToString();

                HyperLink hlTeamParticpatnCellDivision = new HyperLink();
                hlTeamParticpatnCellDivision.Text = teamDTO[0].AgeDivision;

                HyperLink hlTeamParticpatnCellRole = new HyperLink();
                hlTeamParticpatnCellRole.Text = allParticipationOfTeamMemberOfOnePage[i].Role;

                queryString = Request.QueryString.ToString();



                hlTeamParticpatnCellTeam.NavigateUrl = "viewTeamById.aspx?" +teamDTO[0].TeamId;


                lblParticpatnOfStudent1.Text = "The list of team(s) on which";
                lblParticpatnOfStudent.Text = " was presenting the college";

                if (studentDTO.Count != 0)
                {
                    hlParticpatnOfStudent.Text = studentDTO[0].FullName;
                    hlParticpatnOfStudent.NavigateUrl = "viewStudentById.aspx?" + studentDTO[0].StudentId;
                }
                if (staffDTO.Count != 0)
                {
                    hlParticpatnOfStudent.Text = staffDTO[0].FullName;
                    //this should be redirected to staff page
                   // hlParticpatnOfStudent.NavigateUrl = "viewStudentById.aspx?" + staffDTO[0].StaffId;
                }



                List<HyperLink> allHyperlinks = new List<HyperLink>();
                allHyperlinks.Add(hlTeamParticpatnCellTeam);
                allHyperlinks.Add(hlTeamParticpatnCellYr);
                allHyperlinks.Add(hlTeamParticpatnCellDivision);
                allHyperlinks.Add(hlTeamParticpatnCellRole);
                
                   


                foreach (HyperLink hyperlink in allHyperlinks)
                {
                    TableCell tc = new TableCell();



                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;

                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hyperlink);


                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table

                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
    }
}